<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"          
          xmlns:c="http://java.sun.com/jsp/jstl/core"
          version="2.0">

    <jsp:directive.page import="java.util.ArrayList"/>
    <jsp:directive.page import="java.util.Iterator"/>
    <jsp:directive.page import="entity.Users"/>
    <jsp:directive.page import="java.util.List"/>

    <jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>



    <html>
        <head>
            <meta charset="utf-8"/>
            <link rel="stylesheet"  href="css/admin_style.css"  type="text/css" />
            <link rel="stylesheet"  href="css/all.css"  type="text/css" />
            <script type="text/javascript">
                <jsp:include page="scripts/adminValidator.js" />
            </script>
            <script type="text/javascript">
                <jsp:include page="scripts/jquery-1.8.3.min.js" />
            </script>

            <title>Admin</title>

        </head>
        <body>
            <header>

                <div id ="top_name"> <a href="main.jsp">  <img src="/Delta-war/product/1.jpeg"></img></a> <h1> Admin </h1> </div>
                  
                <div id="registr2">
                 
                    <c:set var="user" scope="session" value="${user}"/>
                    <p> <a href="user_name.jsp">${user.getLogin()}  |</a>
                      
                        <a href="logout">LogOut</a> </p>
                </div>
                    
            
             
            </header>
            <div id="main"> 
                <div id="groupManagement" > 
                    <a href="group.jsp" style="cursor:pointer;font:bold;">Group management</a>
                    <!--<span style="cursor:pointer;font:bold; color: #0000ff;" onclick="document.getElementById('group_menu').style.height = '400px'"> Assign roles/groups </span>-->

                </div>

                <div id="user-panel">  

                    <c:if test="${not empty user }"> 
                        <c:if test="${not empty user.getRoleCollection()}"> 
                            <c:set var="rolecollection" value="${user.getRoleCollection()}"/>
                            <table> <tr>
                                <c:forEach var="r" items="${rolecollection}">
                                    <c:set var="rolename" value="${r.getRoleName()}"/>
                                   
                                        <c:if test="${rolename eq 'Warehouse Employee'}">
                                            <td><a href="/Delta-war/ViewWarehouse">Warehouse dashboard</a></td></c:if>

                                        <c:if test="${rolename eq 'Courier'}">
                                            <td><a href="/Delta-war/courier">Courier dashboard</a></td> </c:if>
                                        <c:if test="${rolename eq 'Customer'}">
                                            <td><a href="/Delta-war/user_name.jsp">Customer</a></td></c:if>
                                       
                                </c:forEach> </tr></table>
                        </c:if></c:if></div>
                    <div id="find" >   
                        Find by Name(or Surname): <br/>
                        <form method="GET" action="userview"  onsubmit="return checkFind();"> 
                            <input  id="keyword" type="text" size="30" name="keyword"/>
                            <button type="submit" style="width:65px;    background: #b8b0fd;"> Search </button>
                        </form>
                    </div>
                    <div id="warningFind" style="position:relative; top: 15px;"><c:out value=""/></div>  
                <div id="sorted" > 
                    Sort by:
                    <form method='GET' action='userview'>
                        <select name="SortBy"  size = '1'> 
                            <option value="LastName" >Last name</option>
                            <option value="FirstName" >First name</option>
                            <option value='Email'>E-mail</option>
                            <option value='Login'>Login</option>
                        </select> 
                        <input type="submit" value="Sort" style=" margin-left: 10px; width:65px;   background: #b8b0fd;"/> 
                    </form>
                </div>


                <div id="tabl">
                    <c:if test="${not empty users}">
                        <form method="Get" id="mainForm" > 
                            <table border="2">

                                <TR bgcolor="#b8b0fd">
                                    <td id="check"><strong>Check</strong></td>
                                    <td><strong> Name </strong></td>
                                    <td><strong> Last Name </strong></td>
                                    <td><strong> Address </strong></td>
                                    <td><strong> E-mail </strong></td>
                                    <td><strong> Group </strong></td>
                                    <td><strong> User's login </strong></td>

                                </TR>
                                <c:set var="totalCount" scope="session" value="${count}"/>
                                <c:set var="perPage" scope="session" value="5"/>
                                <c:set var="totalPages" scope="session" value="${totalCount/perPage}"/>
                                <c:set var="pageIndex" scope="session" value="${param.start/perPage+1}"/>

                                <c:if test="${!empty param.start and param.start >(perPage-1) and param.start !=0 }">
                                    <c:set var="url" scope="page" value="?start=${param.start - perPage}"/>
                                    <a href="${url}" value="Prev"/>

                                </c:if>
                                <c:forEach var="user" items="${users}">

                                    <tr>
                                        <td><input  name="IdUser" type="checkbox" class="IdUser" value="${user.getIdUser()}"/>
                                        </td>
                                        <td ><c:out value="${user.getFirstName()}"/></td>
                                        <td ><c:out value="${user.getLastName()}"/></td>
                                        <td ><c:out value="${user.getAddress()}"/></td>
                                        <td ><c:out value="${user.getEMail()}"/></td>
                                        
                                        <c:set var="groupinfo" scope="request" value="user_group?IdUser=${user.getIdUser()}"/>
                                        <td> <a href="${groupinfo}">More detail</a></td>
                                        <td ><c:out value="${user.getLogin()}"/></td>

                                    </tr>
                                </c:forEach>
                            </table>
                            <c:if test = "${not empty myuser}" > 
                                <a style="font-weight: bold"><c:out value = "${myuser.getLastName()} ${myuser.getFirstName()} group info"/></a>
                                <table>

                                    <tr>
                                        <td  height="20" bgcolor="#b8b0fd" align="middle"><strong>Group</strong></td>
                                        <td  height="20" bgcolor="#b8b0fd" align="middle"><strong>Roles</strong></td>
                                    </tr>
                                    <tr>
                                        <td >
                                            <c:set var="usergroups" scope="page" value="${myuser.getGroupsCollection()}"/>
                                            <c:if test="${not empty usergroups}"> 
                                                <select>
                                                    <c:forEach var="g" items="${usergroups}">
                                                        <option>
                                                            <c:out value="${g.getGroupName()}"/></option>
                                                        </c:forEach>
                                                </select>
                                            </c:if>
                                            <c:if test="${empty usergroups}"> None </c:if > 

                                            </td>
                                            <td width="100" height="20" bgcolor="#b8b0fd" align="middle">

                                            <c:set var="mroles" scope="page" value="${myuser.getRoleCollection()}"/>
                                            <c:if test="${not empty mroles}"> 
                                                <select>
                                                    <c:forEach var="mrole" items="${mroles}">
                                                        <option ><c:out value="${mrole.getRoleName()}"/></option>
                                                    </c:forEach>
                                                </select>
                                            </c:if>
                                            <c:if test="${empty mroles}"> None </c:if>
                                            </td>                                      
                                        </tr>  
                                    </table>
                            </c:if> 
                            <table  cellpadding="5" cellspacing="5">
                                <c:if test="${currentPage != 1}">
                                    <td><button type="submit" style="width:80px;    background: #b8b0fd;" >
                                            <a href="userview?page=${currentPage - 1}">&lt;&lt;</a></button></td> 
                                        </c:if>

                                <td>
                                    <c:forEach begin="1" end="${noOfPages}" var="i">
                                        <c:choose>
                                            <c:when test="${currentPage eq i}">
                                                <div><td>${i}</td></div>
                                            </c:when>
                                            <c:otherwise>
                                            <td><a href="userview?page=${i}">${i}</a></td>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:forEach>
                                </td>
                                <c:if test="${currentPage lt noOfPages}">
                                    <td><button type="submit" style="width:80px;    background: #b8b0fd;" >  <a href="userview?page=${currentPage + 1}">&gt;&gt;</a></button></td>
                                </c:if>
                            </table>

                        </form>
                    </c:if>
                      <div style="position: relative; top: 20px; z-index: 24;"><c:if test="${empty users}"> 
                      <c:out value="Sorry. No user information"/></c:if></div>
                    </div>

                    <div id="button1"> <button type="submit" onclick="addUser()"  > Add </button> </div>
                    <div id="button1"> <button type="submit" onclick="deleteUser()"  > Delete </button> </div>
                    <div id="button1"> <button type="submit"  onclick="editUser()" >Edit </button> </div>
                    <div id="button2"> <button type="submit" onclick="refreshUser()"  > Refresh </button> </div>
                    <span id="warning" style="color: red; margin-left: 40%"></span><br/>
                    <div>

                        <div id="group_menu" style="margin-top: 20px">
                            <br/>
                            <br/>
                            <br/>
                            <div id="group_tabl">
                                <strong>Groups</strong>
                                <form method="POST" action="AssignUsers">

                                    <table border="2">
                                        <TR>
                                            <td></td>
                                            <td width="100" bgcolor="#b8b0fd" ><strong> Name </strong></td>
                                            <td width="100" bgcolor="#b8b0fd" ><strong> Roles </strong></td>
                                        </TR>
                                    <c:if test="${not empty mygroups}"> 
                                        <c:forEach var="group" items="${mygroups}">
                                            <tr>
                                                <td >
                                                    <input  class="idGroup" name="idGroup"  type="radio" value="${group.getIdGroup()}"/></td>
                                                <td>
                                                    <c:out value="${group.getGroupName()}"/></td>
                                                <td >
                                                    <c:if test="${not empty group.getRoleCollection()}"> 
                                                        <select>
                                                            <c:forEach var="grole" items="${group.getRoleCollection()}">
                                                                <option ><c:out value="${grole.getRoleName()}"/></option>
                                                            </c:forEach>
                                                        </select>
                                                    </c:if>
                                                    <c:if test="${empty group.getRoleCollection()}"> None </c:if>
                                                    </td>
                                                </tr>
                                        </c:forEach>
                                    </c:if>
                                </table>
                                     <table  cellpadding="5" cellspacing="5">
                                <c:if test="${currentGroupPage != 1}">
                                    <td><button type="submit" style="width:80px;    background: #b8b0fd;" >
                                            <a href="userview?groupPage=${currentGroupPage - 1}">&lt;&lt;</a></button></td> 
                                        </c:if>

                                <td>
                                    <c:forEach begin="1" end="${noOfGroupPages}" var="i">
                                        <c:choose>
                                            <c:when test="${currentGroupPage eq i}">
                                                <div><td>${i}</td></div>
                                            </c:when>
                                            <c:otherwise>
                                            <td><a href="userview?groupPage=${i}">${i}</a></td>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:forEach>
                                </td>
                                <c:if test="${currentGroupPage lt noOfGroupPages}">
                                    <td><button type="submit" style="width:80px;    background: #b8b0fd;" >  <a href="userview?groupPage=${currentGroupPage + 1}">&gt;&gt;</a></button></td>
                                </c:if>
                            </table>

                                    
                                <span id="warningGroup" style="color: red; margin-left: 10%; "></span><br/>
                                <span id="infoGroup" style="color: darkblue; margin-left: 10%"></span><br/>

                                <button type="button" name="assignButton" onclick="sendRequest(this)" style="width:100px;    background: #b8b0fd; position: relative; left: 43%; bottom:150px;" > Assign users </button>
                                <button type="button" name="deleteButton" onclick="sendRequest(this)" style="width:100px;    background: #b8b0fd; position: relative; left: 32.5%; bottom:110px; " > Delete users </button> 
                            </form>
                        </div>
                        <div id="group_tabl1" style="margin-top: 70px;">
                            <form method="Post" action="AssignUsers">

                                <strong> Roles </strong>
                                <table border="2">


                                    <td><input  class="idRole" name="idRole"  type="radio" value="1"/></td>
                                    <td   bgcolor="#b8b0fd" >Customer</td>
                                    <td><input  class="idRole" name="idRole"  type="radio" value="2"/></td>
                                    <td   bgcolor="#b8b0fd" >Warehouse Employee</td>
                                    <td><input  class="idRole" name="idRole"  type="radio" value="3"/></td>
                                    <td   bgcolor="#b8b0fd">Courier</td>
                                    <td><input  class="idRole" name="idRole"  type="radio" value="4"/></td>
                                    <td  bgcolor="#b8b0fd" >Administrator</td>


                                </table>
                                <button type="button" name="assignRole" onclick="sendRequest(this)" style="width:100px;    background: #b8b0fd; position: relative; left: 103.5%; bottom:40px;" > Assign role</button> 
                                <span id="warningRole" style="color: red; margin-left: 40%"></span><br/>
                                <span id="infoRole" style="color: darkblue; margin-left: 40%"></span><br/> 
                            </form>
                        </div>
                    </div>
                </div>
            </div>
                <div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>
        </body>
    </html>
</jsp:root>